package cn.ronghui.project.infrastructure.adapter.repository;

import cn.ronghui.project.domain.student.adapter.repository.IStudentRepository;
import cn.ronghui.project.domain.student.model.entity.StudentEntity;
import cn.ronghui.project.infrastructure.dao.IStudentDao;
import cn.ronghui.project.infrastructure.dao.po.Student;
import cn.ronghui.project.types.exception.AppException;
import org.springframework.stereotype.Repository;

@Repository
public class StudentRepository implements IStudentRepository {

    private final IStudentDao studentDao;

    public StudentRepository(IStudentDao studentDao) {
        this.studentDao = studentDao;
    }

    @Override
    public StudentEntity queryStudentInfo(String studentId) {
        Student student = studentDao.queryStudentInfo(studentId);

        if(null == student){
            return null;
        }

        StudentEntity studentEntity = StudentEntity.builder()
                .name(student.getName())
                .gender(student.getGender())
                .studentId(student.getStudentId())
                .birthDate(student.getBirthDate())
                .enrollmentDate(student.getEnrollmentDate())
                .graduationDate(student.getGraduationDate())
                .homeAddress(student.getHomeAddress())
                .build();

        return studentEntity;
    }

    @Override
    public void updateStudentInfo(StudentEntity studentEntity) {

        if(null == studentEntity){
            throw new AppException("空指针异常");
        }

        Student student = Student.builder()
                .studentId(studentEntity.getStudentId())
                .gender(studentEntity.getGender())
                .name(studentEntity.getName())
                .homeAddress(studentEntity.getHomeAddress())
                .birthDate(studentEntity.getBirthDate())
                .graduationDate(studentEntity.getGraduationDate())
                .enrollmentDate(studentEntity.getEnrollmentDate())
                .build();
        int updateInfoCount = studentDao.updateStudentInfo(student);
        if(updateInfoCount <= 0){
            throw new AppException("更新学生信息失败");
        }
    }
}
